package com.future.heatwave.network

import android.util.Log
import okhttp3.Interceptor
import okhttp3.Interceptor.*
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import java.io.IOException
import java.util.concurrent.TimeUnit


object OkHttpManagerLockFreeList {
    const val TAG = "OkHttpManager"
    const val RETRY_COUNT = 1  // 重连次数
    private const val TIME_OUT = 60L
    private val defaultOkHttpClient by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        val builder = OkHttpClient.Builder()
            .callTimeout(TIME_OUT, TimeUnit.SECONDS)
            .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
            .readTimeout(TIME_OUT, TimeUnit.SECONDS)
            .writeTimeout(TIME_OUT, TimeUnit.SECONDS)
            .retryOnConnectionFailure(false)

        val loggingInterceptor = HttpLoggingInterceptor { message -> Log.i("okHttp", message) }
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
        builder.addInterceptor(loggingInterceptor)
        builder.addInterceptor { chain ->
            val original: Request = chain.request()
            val request = original.newBuilder()
                .method(original.method, original.body)
                .build()
            chain.proceed(request)
        }
        builder.addInterceptor(HeaderInterceptor())

        builder.build()
    }


    private class HeaderInterceptor() : Interceptor {
        @Throws(IOException::class)
        override fun intercept(chain: Chain): Response {
            val originalRequest = chain.request()
            val timestamp =  System.currentTimeMillis().toString()
//            Log.e("bLog", "appInfo *********  ${timestamp}")
            val newRequest = originalRequest.newBuilder()
//                .addHeader("Accept-Encoding", "gzip")
                .addHeader(
                    "appInfo",
                   "ca858a89-9647-35ed-8cdc-b4b4f901e641&com.future.heatwave&V2.2.7"
//                    App.getInstance().getAppInfo(timestamp)
                )
//                .addHeader("Authorization", App.getInstance().getAuthorization(timestamp))
                .addHeader("Authorization", "A26B92882EC0676056B88FB20CC323F8")
                .addHeader("Connection", "Keep-Alive")
                .addHeader("Host", "api.heatshort.com")
                .addHeader("signCode", "10000")
                .addHeader("timestamp","1715067337842")
                .addHeader("User-Agent", "okhttp/4.11.0")
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Content-Length", "0")
                .build()


            return chain.proceed(newRequest)
        }
    }


    fun getOkHttpClient(): OkHttpClient {
        val okHttpClient = getCustomOkHttpClient()
        if (null != okHttpClient) {
            return okHttpClient
        }
        return defaultOkHttpClient.newBuilder().addInterceptor(getCustomInterceptor()).build()
    }

    /**
     * 子类可以重写此方法，返回自定义的 OkHttpClient 对象
     */
    fun getCustomOkHttpClient(): OkHttpClient? {
        return null
    }

    /**
     * 子类可以重写此方法，返回自定义的 Interceptor 对象
     */
    fun getCustomInterceptor(): Interceptor = Interceptor { chain ->
        chain.proceed(chain.request())
    }
}